package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.app.po.RedEnvelopeMoney;
import com.letoken.platform.repo.app.po.RedEnvelopeMoneyPage;
import com.letoken.platform.repo.app.po.RedEnvelopePage;
import com.letoken.platform.repo.app.po.Statistical;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2023-01-10
 */
public interface RedEnvelopeMoneysMapper extends BaseMapper<RedEnvelopeMoney> {


    int batchInsert(List<RedEnvelopeMoney> redEnvelopeMoneys);


    @Select("select * from red_envelope_moneys where red_envelope_id = #{redEnvelopeId} and user_id = 0  ORDER BY RAND() limit 1")
    RedEnvelopeMoney selectAvailable(@Param("redEnvelopeId") Integer redEnvelopeId);


    @Select("select * from red_envelope_moneys where red_envelope_id = #{redEnvelopeId} and user_id = #{userId} limit 1")
    RedEnvelopeMoney selectSelf(@Param("redEnvelopeId") Integer redEnvelopeId, @Param("userId") Integer userId);

    @Select("select * from red_envelope_moneys where red_envelope_id = #{redEnvelopeId}")
    List<RedEnvelopeMoney> selectByRedEnvelopeId(@Param("redEnvelopeId") Integer redEnvelopeId);

    @Select("select ifnull(sum(money),0) from red_envelope_moneys where red_envelope_id = #{redEnvelopeId} and user_id = 0")
    BigDecimal sumByInfoId(@Param("redEnvelopeId") Integer redEnvelopeId);


    @Select("select ifnull(count(id),0) count, ifnull(sum(money),0) sum from red_envelope_moneys where user_id = #{userId}")
    Statistical countAndSumByUserId(Integer userId);

    Page<RedEnvelopeMoneyPage> page(@Param("page") Page<?> page, @Param("userId") Integer userId, @Param("startDatetime") LocalDateTime startDatetime, @Param("endDatetime") LocalDateTime endDatetime);


    Page<RedEnvelopeMoneyPage> pageAlreadyReceived(@Param("page") Page<?> page, @Param("redEnvelopeId") Integer redEnvelopeId);


}
